#ifndef __CHELPER_MEMORY_MEMORYOBJECT_H__
#define __CHELPER_MEMORY_MEMORYOBJECT_H__

#include <assert.h>
#include <iostream>
#include <unordered_map>
#include <string.h>

#include "memory/MemoryBlock.h"


namespace chelper {
namespace memory {

class MemoryObject {
    public:
        template<class T>
        static T *CreateObject() {
            if (block == nullptr) {
                return nullptr;
            }

            char *place = block->malloc(sizeof(T));
	        return (T *) new(place) T;
        }

        template<class T>
        static void Destroy(T *obj) {
            if (block == nullptr || obj == nullptr) {
                return;
            }
            obj->~T();
            block->free((char *)obj);
        }

        static void init(uint64_t size_t) {
            if (block != nullptr) {
                return;
            }

            block = new MemoryBlock();
            block->allocateMemory(size_t);
        }

        static void free() {
            if (block == nullptr) {
                return;
            }
            delete block;
            block = nullptr;
        }

    private:
        static MemoryBlock *block;
};

MemoryBlock *MemoryObject::block = nullptr;

}  // namespace memory
}  // namespace chelper

#endif  // __CHELPER_MEMORY_MEMORYOBJECT_H__